<!--
File description:

This script is called after a user has clicked on a search result from searchSholem.cgi, or is called from validate.php which is a 1 time redirect loop from this page 
(i.e. user.php redirects to validate.php which returns back to user.php) This page essentially receives the specific book and page passed from the search results,
and loads the contents of that file (from the server) into a modifiable text area.  The user can then make changes to the text, and submit the changes 
for crowdsourcing approval.

The layout of the page prints 2 text areas, one for changes and the other as a reference to the original state.

 -->

<?php 
//We need sessions on this page since we do not want any non-logged in members accessing this page. Session will track logged in status.
session_start(); 

//Check for logged in status, if not logged in, redirect to log in page.
if($_SESSION["logged_in"] == false){
    header("Location: sholem_login.php");
    exit;
}
//Error check to see if correct book has been referred for this file to use.  If not, redirect to the search
if( !isset($_GET['book'])){
    header("Location: searchSholem.cgi");
    exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <style>
	.top a {
                text-decoration: none;
                color: #fff;
        }

        .top a:hover {
                color: #c6c9c3;
        }
    </style>
</head>
<body>
    <div class="top bg-primary col-sm-12 text-center">
        <?php echo $_GET['msg'];?>

        <!-- Header -->
        <div class="top container col-md-offset-3 col-md-6 text-center bg-primary">
            <a href="searchSholem.cgi"><h1>All Works of Sholem Aleichem</h1></a>
            <h4>
                <a href="searchSholem.cgi">Search</a> | 
                <?php if($_SESSION["permission"] == 2){
                    echo " <a href = 'admin.php'>Admin</a> | ";
                }?>
                <a href="logout.php">Logout</a>
	    </h4>
        </div>
    </div>
    <!-- Displays current volume/page that is being edited -->
    <div class="current-edit container text-center">
        <h3>Currently Editing: Volume 1, Book 16</h3>
    </div>
<?php
//Now that the HTML layout has been printed, we need obtain the specific OCR file and display it.

//The following php file is needed for getters/setters of directories, running remote server commands,  
require_once "utility.php";
require_once "globals.php";


//This variable holds the directory (the book) of the containing OCR file
$book = $_GET['book']; 

//This variable holds the exact OCR file, ie the page
$page = $_GET['page'];

//This variable holds the name of the user that edited the file
$user = $_SESSION['first'].$_SESSION['last'];

//This variable holds the absolute Multilab file path of the where the currently edited user file is, in other words this is where the file being edited is stored on the server. (this will have to be passed via cgi search function)
$locationOfOcrFile = $locationOfAllOcrFiles."/".$book."/".$page;


//Now read the OCR file contents into a string to display for user to make changes
$displayOcr = file_get_contents($locationOfOcrFile);
//If the file could not be opened, display error to user informing which file is corrupt
if (!$displayOcr){
    $displayOcr = "Failed to load OCR file ".$locationOfOcrFile.". Please refresh the page. If this continues, contact the administrator.";
}


//Display the contents of the file that the user wants to make changes to.  After the user is finished making changes, they press submit, in which this script 
//jumps to validate.php
echo "
<!-- Editing format to make OCR changes. User edits text area on right side -->
<div class='row'>

    <div class='col-md-offset-1 col-md-5 text-center'>
        <form>
            <div class='form-group'>
                <img src='ocr/nybc202583_0016.jpg' alt='Book/Page' width='550'>
            </div>
        </form>
    </div>

    <div class='col-md-5 text-center'>
        <form action='validate.php' method='POST'>
            <div class='form-group text-right'>
                <textarea class='form-control text-right' rows='35' name='editedText'>
	            " .
	            $displayOcr
	            . "
	        </textarea>
		<input type='hidden' name='user' value='".$user."'>
		<input type='hidden' name='book' value='".$book."'>
		<input type='hidden' name='page' value='".$page."'>
            </div>
	    <!-- Submit OCR changes -->
	    <button class='btn btn-primary' type='submit' name='ocrMakeChange'>Submit OCR Changes</button>
        </form>
    </div>
</div>

<!-- Latest compiled and minified JavaScript -->
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script>
";


?>


</body>
</html>


